﻿using System;
using System.Web;
using System.Text;
using System.Collections.Generic;

using Model.Cms;
using Com.Ompchina.Lib.Text;
namespace Util.Helper
{
    /// <summary>
    /// 核心功能类，辅助操作类，管理员辅助操作
    /// </summary>
    public class WebHelper
    {
        /// <summary>
        /// 转换区块内容
        /// </summary>
        /// <param name="info"></param>
        /// <param name="Server"></param>
        /// <returns></returns>
        public string ConvertBlock(HttpServerUtility Server, BlockInfo info)
        {
            int[] size = new int[2];
            if (new Regular().MatchString(info.BlockSize, @"\d+x\d+"))
            {
                string[] s = info.BlockSize.Split('x');
                size[0] = int.Parse(s[0]);
                size[1] = int.Parse(s[1]);
            }
            switch (info.BlockType)
            {
                case "t": return Server.HtmlDecode(info.Content);
                case "i":
                    {
                        if (string.IsNullOrEmpty(info.Link))
                            return string.Format("<img alt=\"{0}\" src=\"{1}\" {2} />", info.BlockName, info.Filepath, size[0] > 0 ? string.Format("width=\"{0}\"", size[0]) : "");
                        else
                            return string.Format("<a target=\"_blank\" href=\"{0}\"><img alt=\"{1}\" src=\"{2}\" {3} /></a>", info.Link, info.BlockName
                                , info.Filepath, size[0] > 0 ? string.Format("width=\"{0}\"", size[0]) : "");
                    };
                case "f": return string.Format("<div id=\"{0}\"></div><script type='text/javascript'>swfobject.embedSWF(\"{1}\", \"{0}\", \"{2}\", \"{3}\", \"9.0.0\");</script>",
                    "block_" + info.BlockID, info.Filepath, size[0], size[1]);
                default: return "";
            }
        }

        /// <summary>
        /// 转换文件前缀
        /// </summary>
        /// <param name="str">原始字符串</param>
        /// <param name="search">搜索字符串</param>
        /// <param name="replace">替换字符串</param>
        /// <returns></returns>
        public string ConvertPrefix(string str, string search, string replace) 
        {
            return str.Replace(search, replace);
        }

        /// <summary>
        /// 裁剪字符串
        /// </summary>
        /// <param name="str">字符串原文</param>
        /// <param name="len">字符串长度</param>
        /// <returns></returns>
        public string CutString(string str, int len) 
        {
            if (str.Length <= len)
                return str;
            else
            {
                return str.Substring(0, len - 1) + "…";
            }
        }
    }
}
